package com.maya.screens;

import com.maya.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
 
public class ProtocolTypesDialogRadio extends DialogFragment
{
    AlertPositiveListener alertPositiveListener;

    interface AlertPositiveListener {
        public void onPositiveClickInProtocolTypes(int position);
    }
 
    /** This is a callback method executed when this fragment is attached to an activity.
    *   This function ensures that, the hosting activity implements the interface AlertPositiveListener * */
    public void onAttach(android.app.Activity activity) 
    {
        super.onAttach(activity);
        try{
            alertPositiveListener = (AlertPositiveListener) activity;
        }catch(ClassCastException e){
            // The hosting activity does not implemented the interface AlertPositiveListener
            throw new ClassCastException(activity.toString() + " must implement AlertPositiveListener");
        }
    }
 
    /** This is the OK button listener for the alert dialog, which in turn invokes the method onPositiveClick(position)*/
    OnClickListener positiveListener = new OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) {
            AlertDialog alert = (AlertDialog)dialog;
            int position = alert.getListView().getCheckedItemPosition();
            alertPositiveListener.onPositiveClickInProtocolTypes(position);
        }
    };
 
    /** This is a callback method which will be executed on creating this fragment */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {
        Bundle bundle = getArguments();
        int position = bundle.getInt("position");
 
        AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
        
        b.setSingleChoiceItems(Constants.TWO_PLAYERS_PROTOCOL_TYPES, position, null);
        b.setPositiveButton(R.string.OK,positiveListener);
        b.setNegativeButton(R.string.Cancel, null);
        
        AlertDialog d = b.create();
 
        return d;
    }
}